package com.sakura.test;

import com.sakura.bean.Yellow;
import com.sakura.config.MainConfigOfProFile;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.sql.DataSource;

/**
 * @author Sakura
 * @Date 2023-02-24 15:55
 */
public class IOCTest_Profile {

    //1、使用命令行动态参数: 在虚拟机参数位置加载 -Dspring.profiles.active=test
    //2、代码的方式激活某种环境；
    @Test
    public void testProfile(){
//        AnnotationConfigApplicationContext configApplicationContext
//                =new AnnotationConfigApplicationContext(MainConfigOfProFile.class);

        //代码方式进行激活某种配置环境
        AnnotationConfigApplicationContext configApplicationContext
                =new AnnotationConfigApplicationContext();
        //1、创建一个applicationContext
        //2、设置需要激活的环境
        configApplicationContext.getEnvironment().setActiveProfiles("dev");
        //3、注册主配置类
        configApplicationContext.register(MainConfigOfProFile.class);
        //4、启动刷新容器
        configApplicationContext.refresh();

        String[] beanNamesForType = configApplicationContext.getBeanNamesForType(DataSource.class);
        for (String name : beanNamesForType){
            System.out.println("name = " + name);
        }

        Yellow bean = configApplicationContext.getBean(Yellow.class);
        System.out.println("bean = " + bean);

        configApplicationContext.close();
    }
}
